module YamlFormBuilder
  class YamlForm < ActionView::Helpers::FormBuilder

    def render_form(name)
      yaml_init('formconfig.yml')
      s = "<table> \n"      
      @config[name].each do |w|
        s+="<tr><td>"+w['label'].to_s+"</td><td>"
        input = w['input']        
        case w['type']
          when 'text'
            s+=text_field(input)
          when 'textarea'
            s+=text_area(input)
          when 'checkbox'
            s+=check_box(input)
          when 'select'
            op = object.send(input)
            s+=select(input,op.collect{|o| [o[w['item']],o[w['val']]]})
          when 'radio'
            s+=radio_button(input,w['value'].to_s)
          when 'password'
            s+=password_field(input)
          when 'country_select'
            s+=country_select(input)          
        end
        s+="</td></tr> \n"     
      end
      s+="<tr><td></td><td><input type=\"submit\" value=\"submit\" /></td></tr>"
      s+"</table>"   
    end
    
    def yaml_init(config_file)
      @config ||= YAML.load_file("#{RAILS_ROOT}/config/#{config_file}")      
    end    
  end
  
  def yaml_form_for(name, object, options, &proc)
    form_for(name, object, options.merge(:builder => YamlForm), &proc)  
  end
  
  def yaml_remote_form_for(name, object, options, &proc)
    remote_form_for(name, object, options.merge(:builder => YamlForm), &proc)  
  end
end